package com.cloud.fim.common.process;


import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 * @since 2019-11-09 21:21
 */
@Slf4j
public abstract class AbstractIMProcessor<K,C,T> implements IMProcessor<C,T> {

    private Map<K, BiConsumer<C,T>> maps;

    public AbstractIMProcessor() {
        this.maps = new HashMap<>();
        registryProcessor();
    }

    @Override
    public void process(C ctx, T t) {
        BiConsumer<C,T> consumer = maps.get(t.getClass());
        if (consumer == null) {
            log.warn("[message processor] unexpected msg: {}", t.toString());
        } else {
            consumer.accept(ctx,t);
        }
    }

    public  void registry(K k, BiConsumer<C,T> consumer) {
        maps.put(k, consumer);
    }

    /**
     * registry processor
     */
    public abstract void registryProcessor();

}
